package common

import (
	"gino/app/constant"
	"github.com/gin-gonic/gin"
	"net/http"
)

type PageResult struct {
	List  interface{} `json:"list"`
	Total int64       `json:"total"`
	Page  int         `json:"page"`
	PageInfo
}

func NewPageResult(list interface{}, total int64, info PageInfo) *PageResult {
	return &PageResult{List: list, Total: total, PageInfo: info}
}

type response struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func ReturnJson(c *gin.Context, httpCode int, code int, msg string, data interface{}) {
	c.JSON(httpCode, response{
		Code: code,
		Msg:  msg,
		Data: data,
	})
}

// Success 直接返回成功
func Success(c *gin.Context, message string, data interface{}) {
	ReturnJson(c, http.StatusOK, constant.SUCCESS, message, data)
}

// Fail 失败的业务逻辑
func Fail(c *gin.Context, code int, message string, data interface{}) {
	ReturnJson(c, http.StatusBadRequest, code, message, data)
	c.Abort()
}

// ReturnJsonFromString 将json字符窜以标准json格式返回（例如，从redis读取json格式的字符串，返回给浏览器json格式）
func ReturnJsonFromString(Context *gin.Context, httpCode int, jsonStr string) {
	Context.Header("Content-Type", "application/json; charset=utf-8")
	Context.String(httpCode, jsonStr)
}
